package cn.anicert.zk.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.anicert.zk.bean.Node;
import cn.anicert.zk.bean.TreeNode;
import cn.anicert.zk.dao.NodeDao;
import cn.anicert.zk.service.TreeNodeService;

@Service
public class TreeNodeServiceImpl implements TreeNodeService {
    @Autowired
    private NodeDao nodeDao;
    
    @Override
    public List<TreeNode> getAllTreeNodeByPath(List<TreeNode> list, String path) {
        List<TreeNode> childNodes = this.getChildTreeNodeByPath(path);
        if (childNodes != null && childNodes.size() > 0) {
            list.addAll(childNodes);
            for (TreeNode node : childNodes) {
                getAllTreeNodeByPath(list, node.getNodePath());
            }
        }
        return list;
    }
    
    @Override
    public List<TreeNode> getChildTreeNodeByPath(String path) {
        List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
        List<Node> nodeList = nodeDao.getNodesByPath(path);
        if (nodeList != null && nodeList.size() > 0) {
            for (Node node : nodeList) {
                //封装TreeNode
                TreeNode treeNode = new TreeNode();
                treeNode.setNodeName(node.getNodeName());
                treeNode.setNodePath(node.getNodePath());
                treeNode.setNodeValue(node.getNodeValue());
                treeNode.setParentNodePath(path);
                treeNode.setParentNode(true);
                treeNodeList.add(treeNode);
            }
        }
        return treeNodeList;
    }
    
    @Override
    public boolean deleteTreeNode(TreeNode treeNode) {
        List<TreeNode> childNodes = this.getChildTreeNodeByPath(treeNode.getNodePath());
        if (childNodes != null && childNodes.size() > 0) {
            for (TreeNode trNode : childNodes) {
                deleteTreeNode(trNode);
            }
            deleteTreeNode(treeNode);
        } else {
            Node node = new Node();
            node.setNodePath(treeNode.getNodePath());
            return nodeDao.deleteNode(node);
        }
        return true;
    }
    
    @Override
    public boolean addTreeNode(TreeNode treeNode) {
        Node node = new Node();
        node.setNodeName(treeNode.getNodeName());
        node.setNodePath(treeNode.getNodePath());
        node.setNodeValue(treeNode.getNodeValue());
        //判断节点是否存在
        return nodeDao.addNode(node);
    }
    
    @Override
    public boolean updateTreeNodeData(TreeNode treeNode) {
        Node node = new Node();
        node.setNodePath(treeNode.getNodePath());
        node.setNodeValue(treeNode.getNodeValue());
        return nodeDao.updateNode(node);
    }
    
    @Override
    public Node readNode(String path) {
        Node node = nodeDao.readData(path);
        return node;
    }
    
    @Override
    public boolean isExistsTreeNode(TreeNode treeNode) {
        return nodeDao.isExists(treeNode.getNodePath());
    }
    
}
